package fun.lifepoem.blog.service;

import fun.lifepoem.blog.domain.CommitRecord;
import fun.lifepoem.blog.mapper.CommitRecordMapper;
import fun.lifepoem.blog.utils.GitUtils;
import fun.lifepoem.blog.utils.SnowFlakeUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.diff.DiffEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Yiwyn
 * @create 2022/11/5 13:28
 */
@Service
@Slf4j
public class GitService {

    @Autowired
    private GitUtils gitUtils;

    @Autowired
    private CommitRecordMapper commitRecordMapper;

    public void recordCommits() throws Exception {
        Date now = new Date();
        List<DiffEntry> gitLog = gitUtils.getGitLog();
        log.info("获取git commit记录：{}", gitLog);
        SnowFlakeUtils snowFlakeUtils = SnowFlakeUtils.Instance();
        List<CommitRecord> records = gitLog.stream().map(p -> {
            CommitRecord commitRecord = new CommitRecord();
            commitRecord.setId(String.valueOf(snowFlakeUtils.getSnowId()));

            DiffEntry.ChangeType changeType = p.getChangeType();
            commitRecord.setType(changeType.toString());
            switch (changeType) {
                case ADD:
                    commitRecord.setPath(p.getNewPath());
                    commitRecord.setGroupCode(p.getNewMode().toString());
                    break;
                case COPY:
//                    buf.append(oldPath + "->" + newPath);
                    break;
                case DELETE:
                    commitRecord.setPath(p.getOldPath());
                    commitRecord.setGroupCode(p.getOldMode().toString());
                    break;
                case MODIFY:
                    commitRecord.setPath(p.getOldPath());
                    commitRecord.setGroupCode(p.getOldMode().toString());
                    break;
                case RENAME:
//                    buf.append(oldPath + "->" + newPath);
                    break;
            }
            commitRecord.setCreateDate(now);
            commitRecord.setUseFlag(false);
            return commitRecord;
        }).collect(Collectors.toList());

        //保存之前需要先判断这次的commit记录有没有保存过。
        List<CommitRecord> exits = commitRecordMapper.selectByCommitRecord(new CommitRecord() {{
            setUseFlag(false);
        }});
        //已经存在的记录
        List<String> exitsCodes = exits.stream().map(CommitRecord::getGroupCode).distinct().collect(Collectors.toList());
        exitsCodes = Optional.of(exitsCodes).orElse(new ArrayList<>());
        List<String> finalExitsCodes = exitsCodes;
        //过滤出当前没有保存过的记录
        List<CommitRecord> saveRecords = records.stream().filter(p -> !finalExitsCodes.contains(p.getGroupCode())).collect(Collectors.toList());


        int row = commitRecordMapper.insertBatch(saveRecords);
        log.info("添加commit记录成功：effect row:{}", row);

    }

}
